import axios from "axios";
import qs from "qs";

let baseUrl = ""
let imgPre = ""
//配置环境
if(process.env.NODE_ENV === "development"){
    //开发
    baseUrl = "/api";
    imgPre = "http://localhost:2000";
}else{
    //生产

}
export {imgPre};
//请求拦截
axios.interceptors.request.use(confirm=>{
    // 补全链接
    confirm.url=baseUrl +confirm.url;
    return confirm
})

//响应拦截
axios.interceptors.response.use(res=>{
    return res;
})

//get请求
export const get = (url,params={})=>{
    return axios({
        url,
        params
    })
}

//post请求

export const post = (url,params={},haveFile=false)=>{
    let data=null;
    if(haveFile){
        data=new FormData();
        for(let i in params){
            data.append(i,params[i]);
        }
    }else{
        data=qs.stringify(params);//stringify格式
        // data=params//json格式
    }
    return axios({
        url,
        method:"post",
        data
    })
}